package com.ian.controller;

import com.common.util.ResultMsg;
import com.ian.pojo.PerUser;
import com.ian.service.PerUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户钱包
 */
@Api("用户钱包相关API")
@RestController
public class WalletController {
    @Autowired
    PerUserService perUserService;

    @ApiOperation("查询用户钱包信息")
    @GetMapping(value = "/balance", produces = "application/json;charset=UTF-8")
    public ResultMsg balance(@RequestParam("mobile") String mobile) {
        PerUser perUser = perUserService.findByTel(mobile);
        return ResultMsg.success("查询用户钱包信息成功~", perUser);
    }

    @ApiOperation("钱包余额充值")
    @PutMapping(value = "/uploadBalance", produces = "application/json;charset=UTF-8")
    public ResultMsg uploadBalance(@RequestParam("mobile") String mobile, @RequestParam("money") int money) {
        PerUser perUser = new PerUser();
        PerUser old = perUserService.findByTel(mobile);
        perUser.setPerUserTel(mobile);
        perUser.setPerUserId(old.getPerUserId());
        perUser.setPerUserSex(old.getPerUserSex());
        perUser.setPerUserPwd(old.getPerUserPwd());
        perUser.setPerUserName(old.getPerUserName());
        perUser.setPerUserHeadimg(old.getPerUserHeadimg());
        perUser.setPerUserAddress(old.getPerUserAddress());
        perUser.setPerUserStatus(old.getPerUserStatus());
        perUser.setPerUserMoney(old.getPerUserMoney() + money);
        perUserService.updateByTel(perUser);
        return ResultMsg.success("充值成功~", perUser);
    }

    @ApiOperation("钱包余额提现")
    @PutMapping(value = "/getBalance", produces = "application/json;charset=UTF-8")
    public ResultMsg getBalance(@RequestParam("mobile") String mobile, @RequestParam("money") int money) {
        PerUser perUser = new PerUser();
        PerUser old = perUserService.findByTel(mobile);
        perUser.setPerUserTel(mobile);
        perUser.setPerUserId(old.getPerUserId());
        perUser.setPerUserSex(old.getPerUserSex());
        perUser.setPerUserPwd(old.getPerUserPwd());
        perUser.setPerUserName(old.getPerUserName());
        perUser.setPerUserHeadimg(old.getPerUserHeadimg());
        perUser.setPerUserAddress(old.getPerUserAddress());
        perUser.setPerUserStatus(old.getPerUserStatus());
        perUser.setPerUserMoney(old.getPerUserMoney() - money);
        perUserService.updateByTel(perUser);
        return ResultMsg.success("提现成功~", perUser);
    }
}
